package jackson.src.day05;

import java.util.*;

/*
 * Author: JacksonCoder
 * FileName: SetDemo
 * Version: 1.0.0
 */
public class SetDemo {
    public static void main(String[] args) {
        List<Message> received = List.of(
                new Message(1, "Hello!"),
                new Message(2, "发工资了吗？"),
                new Message(2, "发工资了吗？"),
                new Message(3, "去哪吃饭？"),
                new Message(3, "去哪吃饭？"),
                new Message(4, "Bye")
        );
        List<Message> displayMessages = process(received);
        for(Message message : displayMessages){
            System.out.println(message.text);
        }
    }

    private static List<Message> process(List<Message> received) {
        Map<Integer, Message> uniqueMessages = new LinkedHashMap<>();
        // 遍历消息列表，将消息按 sequence 放入 Map，后来的会覆盖之前的
        for (Message message : received) {
            uniqueMessages.put(message.sequence, message);
        }
        // 返回去重后的消息列表
        return new ArrayList<>(uniqueMessages.values());
    }
}

class Message {
    public final int sequence;

    public final String text;

    public Message(int sequence, String text){
        this.sequence = sequence;
        this.text = text;
    }
}
